package com.ermao.mymes.service.api.impl.service;

import cn.monitor4all.logRecord.annotation.OperationLog;
import com.ermao.mymes.service.api.domain.BatchSendRequest;
import com.ermao.mymes.service.api.domain.SendRequest;
import com.ermao.mymes.service.api.domain.SendResponse;
import com.ermao.mymes.service.api.impl.domain.SendTaskModel;
import com.ermao.mymes.service.api.service.SendService;
import com.ermao.mymes.support.pipeline.ProcessContext;
import com.ermao.mymes.support.pipeline.ProcessController;
import com.ermao.mymes.common.vo.BasicResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;

/**
 * @author Ermao
 * Date: 2022/5/7 18:51
 */
@Service
public class SendServiceImpl implements SendService {

	@Autowired
	private ProcessController processController;

	@Override
	@OperationLog(bizType = "SendService#send", bizId = "#sendRequest.messageTemplateId", msg = "#sendRequest")
	public SendResponse send(SendRequest sendRequest) {

		SendTaskModel sendTaskModel = SendTaskModel.builder()
				.messageTemplateId(sendRequest.getMessageTemplateId())
				.messageParamList(Collections.singletonList(sendRequest.getMessageParam()))
				.build();

		ProcessContext context = ProcessContext.builder()
				.code(sendRequest.getCode())
				.processModel(sendTaskModel)
				.needBreak(false)
				.response(BasicResultVO.success()).build();

		ProcessContext process = processController.process(context);

		return new SendResponse(process.getResponse().getStatus(), process.getResponse().getMsg());
	}

	@Override
	@OperationLog(bizType = "SendService#batchSend", bizId = "#batchSendRequest.messageTemplateId", msg = "#batchSendRequest")
	public SendResponse batchSend(BatchSendRequest batchSendRequest) {
		SendTaskModel sendTaskModel = SendTaskModel.builder()
				.messageTemplateId(batchSendRequest.getMessageTemplateId())
				.messageParamList(batchSendRequest.getMessageParamList())
				.build();

		ProcessContext context = ProcessContext.builder()
				.code(batchSendRequest.getCode())
				.processModel(sendTaskModel)
				.needBreak(false)
				.response(BasicResultVO.success()).build();

		ProcessContext process = processController.process(context);

		return new SendResponse(process.getResponse().getStatus(), process.getResponse().getMsg());
	}
}
